import java.io.IOException;
import javax.servlet.*;
import javax.servlet.http.*;

public class MoneyServlet extends CommonServlet {

  private static final long serialVersionUID = 1L;
  private static long ID = 0L;
  private static synchronized long nextID() {
	  System.out.println("MoneyServlet.nextID");
	  return ID++;
  }
  private long id = nextID();

  public MoneyServlet() {
	  System.out.println("("+id+"MoneyServlet()");
  }

  public void init() throws ServletException {
	  System.out.println("("+id+")MoneyServlet.init()");
  }

  @Override
  protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
  {
    System.out.println("MoneyServlet() ");
    super.service(request, response);
  }
  
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	  System.out.println("("+id+")MoneyServlet.doGet()");	 
	  
	  boolean isValid = checkPreviousPage(response, new String[] {"name","money"});
		
	  request.setAttribute("mb", mb);
	  mb.populate(request);
	  
	  RequestDispatcher dispatcher = request.getRequestDispatcher("/Money.jsp");
	  dispatcher.include(request, response);
  }

  public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	  System.out.println("MoneyServlet.doPost()");
	  session.setAttribute("previous", "money");
		
		mb.populate(request, mb);
		
		//super.doPost(request, response, mb);
		
		
		String cardNumber = mb.get("cardNumber").trim();
		String confirm = mb.get("confirm").trim();
	
		if(cardNumber.equals("") || cardNumber == null) {
			mb.put("error", "U heeft een ongeldig kaartnummer ingevoerd.");
		} else if(confirm.equals("") || confirm == null) {
			mb.put("error", "U heeft een ongeldige bevestiging ingevoerd.");
		} else {
			mb.put("error", "");
			session.setAttribute("mb", mb);
			response.sendRedirect("order");
		}

		if(mb.containsKey("error")) {
			request.setAttribute("mb", mb);
			doGet(request, response);
		}
  }
  
  public void destroy() {
	  System.out.println("("+id+")MoneyServlet.destroy()");
  }

  public void finalize() throws Throwable {
	  System.out.println("("+id+")MoneyServlet.finalize()");
  }
}
